#include "b2.h"
#include <iostream>
using namespace std;

void b2_init() {}

void b2_run(fifo<D_PIXEL> *rgbY,  fifo<D_PIXEL> *rgbU,  fifo<D_PIXEL> *rgbV,
	    fifo<D_PIXEL> *b2Y1,  fifo<D_PIXEL> *b2Y2,  fifo<D_PIXEL> *b2U,   fifo<D_PIXEL> *b2V) {

  /* need to downsample U and V components,
     and average pixels
  */
  
  int i;
  D_PIXEL avu[64];
  D_PIXEL avv[64];

  while (rgbY->len() >= 128) {
    for (i=0; i<64; i++) {
      b2Y1->write(rgbY->read());
      avu[i] = rgbU->read();
      avv[i] = rgbV->read();
    }

    for (i=0; i<64; i++) {
      b2Y2->write(rgbY->read());
      b2U->write((avu[i] + rgbU->read())/2);
      b2V->write((avv[i] + rgbV->read())/2);
    }
/*    for (i=0; i<64; i++) {
      b2Y1->write(rgbY->read());
      if (i%2==0) {
         avu [(i%8)/2+8*(i/8)] = rgbU->read()/2;
         avv [(i%8)/2+8*(i/8)] = rgbV->read()/2;
      } else {
         avu [(i%8)/2+8*(i/8)] += rgbU->read()/2;
         avv [(i%8)/2+8*(i/8)] += rgbV->read()/2;
      }
    }

    for (i=0; i<64; i++) {
      b2Y2->write(rgbY->read());
      if (i%2==0) {
         avu [4+(i%8)/2+8*(i/8)] = rgbU->read()/2;
         avv [4+(i%8)/2+8*(i/8)] = rgbV->read()/2;
      } else {
         avu [4+(i%8)/2+8*(i/8)] += rgbU->read()/2;
         avv [4+(i%8)/2+8*(i/8)] += rgbV->read()/2;
      }
    }
    for (i=0; i<64; i++) {
      b2U->write(avu[i]);
      b2V->write(avv[i]);
    }
*/
  }

}
